package top.saycode.schedule.base.sql;


import top.saycode.schedule.base.common.TaskStatus;
import top.saycode.schedule.base.common.TaskUnit;
import top.saycode.schedule.base.common.TimeUtils;
import top.saycode.schedule.base.model.TaskDO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TaskSQL {

    public static final String TABLE = """
            CREATE TABLE IF NOT EXISTS task_schedule (
                id                      INT         NOT NULL PRIMARY KEY AUTOINCREMENT,
                name                    TEXT        NOT NULL,
                planed_time             TEXT        NOT NULL,
                -- INTERVAL, MONTH, WEEK, ONCE... see more at class TaskUnit
                unit                    TEXT        NOT NULL,
                -- 超时时最多可重复执行的次数，0 为不执行
                timeout_max_repeat      INT         NOT NULL DEFAULT 1,
                -- 最多允许超时(s), 0 为不设限
                timeout                 BIGINT      NOT NULL DEFAULT 0,
                next_scheduled          TEXT        NOT NULL,
                exec                    TEXT        NOT NULL,
                create_time             TEXT        NOT NULL,
                update_time             TEXT        NOT NULL,
                -- RUNNING, WAITING, STOPPED
                status                  TEXT        NOT NULL,
                description             TEXT
            );
            """;

    public static final String SELECT_ALL = """
            SELECT * FROM task_schedule;
            """;

    public static final String INSERT = """
            INSERT INTO task_schedule (name, planed_time, unit, timeout_max_repeat, timeout, next_scheduled, create_time,
                                        update_time, status, description, exec)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """;
    public static final String DELETE = """
            DELETE FROM task_schedule WHERE id = ?;
            """;


    public static List<TaskDO> convertTaskSchedule(ResultSet resultSet) throws SQLException {
        List<TaskDO> taskDOs = new ArrayList<>();
        while (resultSet.next()) {
            TaskDO taskDO = new TaskDO();
            taskDO.setId(resultSet.getInt("id"));
            taskDO.setName(resultSet.getString("name"));
            taskDO.setPlanedTime(resultSet.getString("planed_time"));
            taskDO.setUnit(TaskUnit.valueOf(resultSet.getString("unit")));
            taskDO.setTimeout(resultSet.getLong("timeout"));
            taskDO.setTimoutMaxRepeat(resultSet.getInt("timeout_max_repeat"));
            taskDO.setNextScheduled(TimeUtils.parseDateTime(resultSet.getString("next_scheduled")));
            taskDO.setExec(resultSet.getString("exec"));
            taskDO.setCreateTime(TimeUtils.parseDateTime(resultSet.getString("create_time")));
            taskDO.setUpdateTime(TimeUtils.parseDateTime(resultSet.getString("update_time")));
            taskDO.setStatus(TaskStatus.valueOf(resultSet.getString("status")));
            taskDO.setDescription(resultSet.getString("description"));
            taskDOs.add(taskDO);
        }
        return taskDOs;
    }

    public static void fillInsertParams(PreparedStatement preparedStatement, TaskDO taskDO) throws SQLException {
        preparedStatement.setString(1, taskDO.getName());
        preparedStatement.setString(2, taskDO.getPlanedTime());
        preparedStatement.setString(3, taskDO.getUnit().name());
        preparedStatement.setInt(4, taskDO.getTimoutMaxRepeat());
        preparedStatement.setLong(5, taskDO.getTimeout());
        preparedStatement.setString(6, taskDO.getNextScheduled().format(TimeUtils.DATE_TIME_FORMATTER));
        preparedStatement.setString(7, taskDO.getCreateTime().format(TimeUtils.DATE_TIME_FORMATTER));
        preparedStatement.setString(8, taskDO.getUpdateTime().format(TimeUtils.DATE_TIME_FORMATTER));
        preparedStatement.setString(9, taskDO.getStatus().name());
        preparedStatement.setString(10, taskDO.getDescription());
        preparedStatement.setString(11, taskDO.getExec());
    }

    public static void fillDeleteParams(PreparedStatement preparedStatement, int id) throws SQLException {}
}
